package org.mspring.mlog.web.module.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Twitter;
import org.mspring.mlog.service.TwitterService;
import org.mspring.mlog.web.query.TwitterQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月30日
 */
@Widget
@RequestMapping("/t")
public class Web_TwitterWidget extends Web_AbstractWidget {
    @Autowired
    private TwitterService twitterService;

    @RequestMapping({ "", "/" })
    public String index(@ModelAttribute Page<Twitter> twitterPage, @ModelAttribute Twitter twitter, @RequestParam Map<String, Object> queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (twitterPage == null) {
            twitterPage = new Page<Twitter>();
        }
        twitterPage.setSort(new Sort("id", Sort.DESC));

        if (twitter == null) {
            twitter = new Twitter();
        }
        twitterPage = twitterService.listPage(new TwitterQueryCriterion(queryParams), twitterPage);
        model.addAttribute("twitterPage", twitterPage);
        return "skin:/t/index";
    }
}
